package com.treasure.mall.zbqmallmanager.base.exception;

import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.exception.ApiException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author allan
 * @date 13/11/2017
 */
@Controller
@ControllerAdvice
public class SMExceptionHandler {
    private static final Log log = LogFactory.getLog(SMExceptionHandler.class);

    /**
     * 所有异常统一处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult runtimeExceptionHandler(Exception ex) {
        if (ex instanceof ApiException) {
            ApiException apiException = (ApiException) ex;
            return apiException.getApiResult();
        }
        return ApiResult.errorWith(ResultCodeEnum.EXCEPTION, ex.getMessage());
    }
}
